<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class AdminModel extends RelationModel{
    protected $_validate = array(
        array('username', 'require', '{%admin_username_empty}'), //不能为空
        array('username', '', '{%admin_name_exists}', 0, 'unique',1), //新增的时候检测重复
    );

	//定义关联表
    protected $_link=array(
        'role'=>array(
            'mapping_type'=>self::BELONGS_TO,
            'foreign_key'=>'role_id',
            'mapping_fields'=>'title',	//要取出的字段
            'as_fields'=>'title:type'	//使用别名
        )
    );
	
    public function name_exists($name, $id=0) {
        $pk = $this->getPk();
        $where = "username='" . $name . "'  AND ". $pk ."<>'" . $id . "'";
        $result = $this->where($where)->count($pk);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
	

}